<?php

namespace App;

use App\Http\Controllers\Api\WeChatThirdController;
use EasyWeChat\Factory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;


/**
 * App\AuthorizerInfo
 *
 * @property int $id
 * @property int $enterprise_id 授权方企业id
 * @property int $user_id 授权方用户id
 * @property string $auth_code 授权方授权码
 * @property string $authorizer_appid 授权方appid
 * @property string $authorizer_access_token 接口调用令牌
 * @property int $expires_in authorizer_access_token 的有效期
 * @property string $authorizer_refresh_token 刷新令牌
 * @property string|null $func_info 授权给开发者的权限集列表
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property int $status 授权状态:0未完成，1已完成，2已取消，3小程序待提交审核，4小程序审核中，5小程序待全网发布，6小程序已上线
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AuthorizerInfo newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AuthorizerInfo newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\AuthorizerInfo onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AuthorizerInfo query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AuthorizerInfo whereAuthCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AuthorizerInfo whereAuthorizerAccessToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AuthorizerInfo whereAuthorizerAppid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AuthorizerInfo whereAuthorizerRefreshToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AuthorizerInfo whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AuthorizerInfo whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AuthorizerInfo whereEnterpriseId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AuthorizerInfo whereExpiresIn($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AuthorizerInfo whereFuncInfo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AuthorizerInfo whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AuthorizerInfo whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AuthorizerInfo whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AuthorizerInfo whereUserId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\AuthorizerInfo withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\AuthorizerInfo withoutTrashed()
 * @mixin \Eloquent
 */
class AuthorizerInfo extends Model
{
    protected $table='authorizer_infos';
    use SoftDeletes;
    protected $fillable=[
        'authorizer_appid',
        'authorizer_access_token',
        'expires_in',
        'authorizer_refresh_token',
        'func_info',
        'status'
    ];
    protected $hidden=[
        'authorizer_access_token',
        'authorizer_refresh_token'
    ];
    public static function getAuthorizerAccessToken($authorizer_appid){
        $data=self::whereAuthorizerAppid($authorizer_appid)->first();
        return $data ? $data->authorizer_access_token : '';
    }

    public static function refreshAccessToken($authorizer_appid){
        //
    }

    /**
     * 获取小程序实例
     * @param $appid
     * @return bool|\EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Application
     */
    public static function getMiniProgramInstance($appid){
        if (!$appid)
            return false;
        $config = WeChatThirdController::getPlatformConfig();
        $openPlatform   = Factory::openPlatform($config);
        $thisProgramRecord=self::whereAuthorizerAppid($appid)->first();
        if (!$thisProgramRecord)
            return false;
        return $openPlatform->miniProgram($thisProgramRecord->authorizer_appid, $thisProgramRecord->authorizer_refresh_token);
    }

    public static function getStatusName($status){
        if ($status==0)
            return '未完成授权';
        if ($status==1)
            return '已完成授权';
        if ($status==2)
            return '授权已取消';
        if ($status==3)
            return '小程序待提交审核';
        if ($status==4)
            return '小程序审核中';
        if ($status==5)
            return '小程序待全网发布';
        if ($status==6)
            return '小程序已上线';
    }

    public static function getRefreshToken($appid){
        $data=AuthorizerInfo::whereAuthorizerAppid($appid)->first();
        if (!$data)
            return '';
        $config=$data;
        return $config ? $config->authorizer_refresh_token : '';
    }
}
